import random


class ATMSystem:
    def __init__(self, bank_name="中国农业银行昌平沙河支行"):
        """
        银行ATM
        :param bank_name: 银行名称
        """
        self.bank_name = bank_name
        self.users = [
            {
                "account": "",
                "account_type": 0,
                "name": "",
                "password": 0,
                "address": {
                    "country": "",
                    "province": "",
                    "city": "",
                    "state": "",
                    "number": ""
                },
                "balance": 0,
                "bank": self.bank_name
            }
        ]
        self.max_users = 101

    def _generate_account_number(self):
        """生成随机账号"""
        while True:
            random_account = random.randint(10000000, 99999999)
            if not any(user["account"] == random_account for user in self.users):
                return random_account

    def _find_user_by_account(self, account):
        """根据账号查找用户"""
        for user in self.users:
            if user["account"] == account:
                return user
        return None

    def _find_user_by_name(self, name):
        """根据姓名查找用户"""
        for user in self.users:
            if user["name"] == name:
                return user
        return None

    def create_account(self, account_type, name, password, country, province, city, state, number):
        """
        开户
        :param account_type: 账户类型
        :param name: 姓名
        :param password: 密码
        :param country: 国家
        :param province: 省份
        :param city: 城市
        :param state: 街道
        :param number: 门牌号
        :return: 1.成功 2.用户已存在 3.用户库已满
        """

        if len(self.users) >= self.max_users:
            print("用户库已满！")
            return 3
        elif self._find_user_by_name(name):
            print("用户已存在！")
            return 2
        else:
            # 添加新用户信息
            new_user = {
                "account": self._generate_account_number(),
                "account_type": account_type,
                "name": name,
                "password": password,
                "address": {
                    "country": country,
                    "province": province,
                    "city": city,
                    "state": state,
                    "number": number
                },
                "balance": 0,
                "bank": self.bank_name
            }
            self.users.append(new_user)
            print(f"开户成功！您的账号是：{new_user['account']}")
            return 1

    def deposit(self, account, amount):
        """
        存钱
        :param account: 账号
        :param amount: 金额
        :return: bool
        """
        if amount < 0:
            print("存款金额不能为负数！")
            return False

        user = self._find_user_by_account(account)
        if user:
            user["balance"] += amount
            print(f"存款成功！当前余额：{user['balance']}元")
            return True
        else:
            print("账号不存在！")
            return False

    def withdraw(self, account, password, amount):
        """
        取钱
        :param account: 账号
        :param password: 密码
        :param amount: 金额
        :return: 0.正常 1.账号不存在 2.密码不对 3.金额不足
        """
        user = self._find_user_by_account(account)
        if not user:
            print("账号不存在！")
            return 1

        if user["password"] != password:
            print("密码错误！")
            return 2

        if user["balance"] < amount:
            print("余额不足！")
            return 3

        user["balance"] -= amount
        print(f"取款成功！当前余额：{user['balance']}元")
        return 0

    def get_account_info(self, account, password):
        """
        查询账户信息
        :param account: 账号
        :param password: 密码
        :return:
        """
        user = self._find_user_by_account(account)
        if not user:
            print("用户不存在！")
            return

        if user["password"] != password:
            print("密码错误！")
            return

        print("=" * 40)
        print("账户信息")
        print("=" * 40)
        print(f"账号：{user['account']}")
        print(f"账户类型：{user['account_type']}")
        print(f"姓名：{user['name']}")
        address = user["address"]
        print(
            f"居住地址：{address['country']}{address['province']}{address['city']}{address['state']}{address['number']}")
        print(f"开户行：{user['bank']}")
        print(f"当前余额：{user['balance']}元")
        print("=" * 40)

    def transfer(self, from_account, from_password, to_account, amount):
        """
        转账
        :param from_account: 转出账户
        :param from_password: 转出账户密码
        :param to_account: 转入账户
        :param amount: 转账金额
        :return: 0.成功 1.账号不存在 2.密码错误 3.金额不足
        """
        from_user = self._find_user_by_account(from_account)
        to_user = self._find_user_by_account(to_account)

        if not from_user or not to_user:
            print("账号不存在！")
            return 1

        if from_user["password"] != from_password:
            print("密码错误！")
            return 2

        if from_user["balance"] < amount:
            print("余额不足！")
            return 3

        # 执行转账
        from_user["balance"] -= amount
        to_user["balance"] += amount
        print(f"转账成功！向账号{to_account}转账{amount}元")
        print(f"您的当前余额：{from_user['balance']}元")
        return 0


